######利用ANOVA检验
cs<-"D://文件//1//汇总.csv"
data1<-read.csv(cs)
data1 <- data1[, -(1:2)]##不读取前2列
colnames(data1) <- substring(colnames(data1), 1, nchar(colnames(data1)) - 2)##删除列名的最后两个字符
group<-colnames(data1)
data1 <- log2(data1)
data1[data1 == -Inf] <- NA
p_values <- numeric(nrow(data1))

for(i in 1:nrow(data1))
{  
row_data <- unlist(data1[i, ])
# 提取每行数据中的不同组
ad_group <- row_data[grep("^ad", group)]
as_group <- row_data[grep("^as", group)]
ct_group <- row_data[grep("^ct", group)]

# 判断每个组是否至少有3个样本是有数值的
if (sum(!is.na(ad_group)) < 3 || sum(!is.na(as_group)) < 3 || sum(!is.na(ct_group)) < 3)
{
  p_values[i] <- NA
} 
else 
{
  anova_result <- oneway.test(row_data ~ group)
  p_values[i] <- anova_result$p.value
}
}
p_values

library(clusterProfiler)
library(org.Hs.eg.db)
library(ggplot2)
library(forcats)
library(dplyr)

load("~/1/volcano.RData")
library(clusterProfiler)
library(org.Hs.eg.db)
library(ggplot2)
load("~/1/volcano.RData")
data2<- prostat[prostat$P < 0.05, "ID"]##提取p<0.05的ID号
gene.df <- bitr(data2,fromType="SYMBOL",toType="ENTREZID", OrgDb = org.Hs.eg.db)##基因id转换，ID类型是ENTREZID
gene <- gene.df$ENTREZID
ego <- enrichGO(gene = gene,
                OrgDb=org.Hs.eg.db,
                keyType = "ENTREZID",
                ont = "ALL",
                minGSSize = 1,
                pvalueCutoff = 0.05,
                qvalueCutoff = 0.2,
                readable = TRUE)##三种类型GO富集分析
ego <- as.data.frame(ego)
# 创建气泡图参考马鉴

library(forcats)

result_BP <- ego %>% filter(ONTOLOGY == 'BP')
result_CC <- ego %>% filter(ONTOLOGY == 'CC')
result_MF <- ego %>% filter(ONTOLOGY == 'MF')

BP <- result_BP[1:10, ]
CC <- result_CC[1:10, ]
MF <- result_MF[1:10, ]####数据太多，仅选择30个

# Convert Description column to factor
BP$Description <- factor(BP$Description)
CC$Description <- factor(CC$Description)
MF$Description <- factor(MF$Description)

all <- rbind(BP, CC, MF)

ggplot(all, aes(x = -log10(pvalue), y = fct_reorder(Description, -log10(pvalue), .fun = median, .na_rm = TRUE), group = ONTOLOGY)) +
  geom_point(aes(size = Count, fill = p.adjust), shape = 21, color = 'black', na.rm = TRUE) +
  facet_grid(ONTOLOGY ~ ., scale = 'free_y', space = 'free_y') +
  scale_fill_gradient(low = 'blue', high = 'red') +
  labs(title = 'GO Enrichment', y = 'GO term', x = '-log10(pvalue)') +
  guides(fill = guide_colorbar(reverse = TRUE, order = 1)) +
  theme_bw() +
  coord_cartesian(xlim = c(0, max(-log10(all$pvalue))))

        